package com.dyz.wsw.domain.admin.repository.persist.impl;

import com.dyz.wsw.domain.admin.pojo.UserLoginInfoVO;
import com.dyz.wsw.domain.admin.repository.persist.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author dyz
 * Time 2024/7/1 19:16
 */
@Slf4j
@Repository
public class
UserRepositoryImpl implements UserRepository {

    private final DatabaseClient databaseClient;

    @Autowired
    public UserRepositoryImpl(DatabaseClient databaseClient){
        log.info("创建用户仓库对象UserRepositoryImpl");
        this.databaseClient = databaseClient;
    }


    @Override
    public Mono<Void> updateLastLogin(Long id, Integer loginCount, String lastLoginIp, LocalDateTime gmtLastLogin) {
        return databaseClient.sql("update admin_user " +
                "set login_count=:loginCount," +
                "last_login_ip=:lastLoginIp," +
                "gmt_last_login=:gmtLastLogin " +
                "where id=:id")
                .bind("id",id)
                .bind("loginCount",loginCount)
                .bind("lastLoginIp",lastLoginIp)
                .bind("gmtLastLogin",gmtLastLogin)
                .then();
    }

    @Override
    public Mono<UserLoginInfoVO> findUserLoginInfoByUsername(String username) {
        log.debug("正在处理【根据用户名查询用户登录信息】的数据访问，用户名：{}", username);
        UserLoginInfoVO userLoginInfoVO = new UserLoginInfoVO(new ArrayList<>());
        return databaseClient.sql("SELECT admin_user.id,admin_user.username," +
                        "admin_user.password,admin_user.avatar,admin_user.enable," +
                        "admin_user.login_count,admin_permission.value " +
                        "FROM admin_user " +
                        "LEFT JOIN admin_user_role ON admin_user.id=admin_user_role.user_id " +
                        "LEFT JOIN admin_role_permission ON admin_user_role.role_id=admin_role_permission.role_id " +
                        "LEFT JOIN admin_permission ON admin_role_permission.permission_id=admin_permission.id " +
                        "WHERE username=:username")
                .bind("username",username)
                .fetch()
                .all()
                .collectList()
                .flatMap(list->{
                    if(!list.isEmpty()){
                        Map<String, Object> ele = list.get(0);
                        userLoginInfoVO.setId(Long.parseLong(ele.get("id").toString()));
                        userLoginInfoVO.setUsername(ele.get("username").toString());
                        userLoginInfoVO.setPassword(ele.get("password").toString());
                        userLoginInfoVO.setAvatar(ele.get("avatar").toString());
                        userLoginInfoVO.setEnable(Integer.parseInt(ele.get("enable").toString()));
                        userLoginInfoVO.setLoginCount(Integer.parseInt(ele.get("login_count").toString()));
                        for (Map<String, Object> stringObjectMap : list) {
                            userLoginInfoVO.getPermissions().add(stringObjectMap.get("value").toString());
                        }
                        return Mono.just(userLoginInfoVO);
                    }else {
                        return Mono.empty();
                    }
                });
    }

    @Override
    public Mono<Void> updateAvatar(Long id, String avatar) {
        return databaseClient.sql("UPDATE admin_user SET avatar=:avatar WHERE id=:id")
                .bind("avatar", avatar)
                .bind("id", id).then();
    }
}
